$(document).ready(function() {
    get_hash();
	$("#datePromoStartDate, #datePromoEndDate, #datePromoStartDateadd, #datePromoEndDateadd").datepicker({ 
		changeYear: true,
	});	
	$("#submitPromo").click(function(){
		savePromo();
	});
	$("#add_promo_form").click(function(){
		addPromoForm();
	});
	$("#submitPromoadd").click(function(){
		addPromo();
	});
	$("#generate_qr_code").click(function(){
		generate_qr_code();
	});
});
function promo(){
	var asort = 0;
	promo_icons = {};
	xfunc = "promo/get_promo_list";
	
	data_sent = {};
		
		$.ajax({ 
			type: "POST", // request type POST
			url: base_url+xfunc, // post to PHP file
			data: data_sent, // request send to PHP file
			dataType:'json',
			success: function(result){ // result = return from php request
				$(result.intID).each(function(k,v){
					asort = result.intID[k];
					promo_icons_panel = '';
						promo_icons_panel +='<tr>';;
							promo_icons_panel +='<td>'+result.strTitle[k]+'</td>';
							promo_icons_panel +='<td><div class="readmore">'+result.strDescription[k]+'</div></td>';
							promo_icons_panel +='<td>'+result.dateStart[k]+'</td>';
							promo_icons_panel +='<td>'+result.dateEnd[k]+'</td>';
							promo_icons_panel +='<td><center><img width="100px" src="'+base_url+'assets/uploaded/img/qrcode_img/'+result.intID[k]+'.png"></center>'+result.strPromoCode[k]+'</td>';
							promo_icons_panel +='<td><center>'+result.intPoints[k]+'</center></td>';
							if(result.intStatus[k]==0){
								var intStatus = "Not Claim";
							} else {
								var intStatus = "Claimed";
							}
							promo_icons_panel +='<td>'+intStatus+'</td>';
							promo_icons_panel +='<td class="controls"><div class="btn-group">';
							//promo_icons_panel +='<button class="btn btn-primary" onclick="javascript:editPromo(\''+result.intID[k]+'\')"><i class="icon-edit"></i>Edit</button>';
							promo_icons_panel +='<button class="btn btn-danger" onclick="javascript:delPromo(\''+result.intID[k]+'\', \''+result.strTitle[k]+'\')"><i class="icon-trash"></i>Delete</button>';
							promo_icons_panel +='</td>';
						promo_icons_panel +='</tr>';
						promo_icons[result.intID[k]]=promo_icons_panel;
				});
				
				promo_html_panel = '<table class="table table-striped table-bordered" id="promo_table">';
				promo_html_panel += '<thead>';
					promo_html_panel += '<tr>';
						promo_html_panel += '<th class="note-title">Name</th>';
						promo_html_panel += '<th class="note-content">Description</th>';
						promo_html_panel += '<th>Date Start</th>';
						promo_html_panel += '<th>Date End</th>';
						promo_html_panel += '<th>QR/Promo Code</th>';
						promo_html_panel += '<th>Status</th>';
						promo_html_panel += '<th>Promo Points</th>';
						promo_html_panel += '<th>Control</th>';
					promo_html_panel += '</tr>';
				promo_html_panel += '</thead>';
				promo_html_panel += '<tbody>';
				
				for(x=0;x<=asort;x++){
					if($.trim(promo_icons[x])!=""){
						promo_html_panel +=promo_icons[x];
					}
								
				}		
				promo_html_panel += '</tbody>';		
				promo_html_panel += '</table>';
				
				$('#promo_content').html(promo_html_panel);
			    $('div.readmore').expander({
			        slicePoint: 120,
			        expandText: '<br /><span class="label label-info">Read More...</span>',
			        userCollapseText: '<br /><span class="label label-important">Hide Text</span>'
			    });
				$('#promo_table').dataTable({
					"sPaginationType": "full_numbers",
					"bLengthChange": false,
					"bSort": false
				});
			}
		});	
}
function generate_qr_code(){
	xfunc = "promo/generate_qr_code";
	data_sent = {};
	
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			$("#strPromoCodeadd").val(result);
			//generated_qr_code(result);
		}
	});
}
/*function generated_qr_code(string){
	xfunc = "promo/generated_qr_code";
	data_sent = {
			'qrCode':string
	};
	
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
		
		}
	});
}*/
function addPromoForm(){
	$("#promo_content, #add_promo_form").hide();
	$("#addPromo").slideDown('slow', function(){
		$("#promo_content").hide();
		$("#addPromo").slideDown('slow', function(){
			$(".back-btn-promoadd a").attr("onclick","btn_back_addpromo()");
		});	
	});	
}
function addPromo(){
	var strTitle = $("#strPromoNameadd").val();
	var strDescription = $("#strPromoDescriptionadd").val();
	var dateStart = $("#datePromoStartDateadd").val();
	var dateEnd = $("#datePromoEndDateadd").val();
	var strPromoCode = $("#strPromoCodeadd").val();
	var intPoints = $("#intPromoPointsadd").val();
	
	dStart = dateStart.split("/");
	adddateStart = dStart[2]+'-'+dStart[0]+'-'+dStart[1]; 
	dEnd = dateEnd.split("/");
	adddateEnd = dEnd[2]+'-'+dEnd[0]+'-'+dEnd[1]; 
	
	if(strTitle==''){
		$("#errorPromo").html("<div class='alert alert-error'>Error: Put the promo name!</div>");
	} else if(strDescription=='') {
		$("#errorPromo").html("<div class='alert alert-error'>Error: Put the promo description!</div>");
	} else if(adddateStart=='') {
		$("#errorPromo").html("<div class='alert alert-error'>Error: Put the promo date start!</div>");
	} else if(adddateEnd=='') {
		$("#errorPromo").html("<div class='alert alert-error'>Error: Put the promo date end!</div>");
	} else if(strPromoCode=='') {
		$("#errorPromo").html("<div class='alert alert-error'>Error: Put the promo code/qr code!</div>");
	} else if(intPoints=='') {
		$("#errorPromo").html("<div class='alert alert-error'>Error: Put the points for promo!</div>");
	} else {
		xfunc = "promo/save_promo";
		data_sent = {
				'strTitle':strTitle,
				'strDescription':strDescription,
				'dateStart':adddateStart,
				'dateEnd':adddateEnd,
				'strPromoCode':strPromoCode,
				'intPoints':intPoints
		};
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				$("#errorPromo").hide();
				$("#addPromoSuccess").html('<div class="alert alert-success">Data was successfully saved!</div>');
				
				$("#strPromoNameadd").val('');
				$("#strPromoDescriptionadd").val('');
				$("#datePromoStartDateadd").val('');
				$("#datePromoEndDateadd").val('');
				$("#strPromoCodeadd").val('');
				$("#intPromoPointsadd").val('');
				$(".alert-success").fadeOut(5000);
			}
		});
	}
}
function editPromo(intID){
	xfunc = "promo/get_promo";
	data_sent = {
			'intID':intID
	};
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){	
			dStart = result.dateStart.split("-");
			dateStart = dStart[1]+'/'+dStart[2]+'/'+dStart[0]; 
			dEnd = result.dateEnd.split("-");
			dateEnd = dEnd[1]+'/'+dEnd[2]+'/'+dEnd[0]; 
			
			$("#intPromoID").val(result.intID);
			$("#strPromoName").val(result.strTitle);
			$("#strPromoDescription").val(result.strDescription);
			$("#strPromoCode").val(result.strPromoCode);
			$("#datePromoStartDate").val(dateStart);
			$("#datePromoEndDate").val(dateEnd);
			$("#intPromoPoints").val(result.intPoints);
		}
	});

	$("#add_promo_form").hide();
	$("#promo_content").hide();
	$("#editPromo").slideDown('slow', function(){
		$(".back-btn-promo a").attr("onclick","btn_back_promo()");
	});	
}
function savePromo(){
	var intID = $("#intPromoID").val();
	var strTitle = $("#strPromoName").val();
	var strDescription = $("#strPromoDescription").val();
	var dateStart = $("#datePromoStartDate").val();
	var dateEnd = $("#datePromoEndDate").val();
	var strPromoCode = $("#strPromoCode").val();
	var intPoints = $("#intPromoPoints").val();

	dStart = dateStart.split("/");
	dateStart = dStart[2]+'-'+dStart[0]+'-'+dStart[1]; 
	dEnd = dateEnd.split("/");
	dateEnd = dEnd[2]+'-'+dEnd[0]+'-'+dEnd[1]; 
	
	xfunc = "promo/save_promo";
	data_sent = {
			'intID':intID,
			'strTitle':strTitle,
			'strDescription':strDescription,
			'dateStart':dateStart,
			'dateEnd':dateEnd,
			'strPromoCode':strPromoCode,
			'intPoints':intPoints
	};
	console.log(data_sent);
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			$("#promoSuccess").html('<div class="alert alert-success">Data was successfully updated!</div>');
			$(".alert-success").fadeOut(5000);
		}
	});
}
function delPromo(intID, titleName){
	xfunc = "promo/delete_promo";
	data_sent = {
			'intID':intID
	};
	$('#label-delete').html(titleName);
    $('#deleteModal').modal('show');
    $('button#confirm').click(function(e){
        $('#deleteModal').modal('hide');
		
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				promo();
			}
		});
    });
}
function btn_back_addpromo(){
	$("#addPromo").hide();
	$("#promo_content").slideDown('slow', function(){
		$("#add_promo_form").show();
		promo();
	});	
}
function btn_back_promo(){
	$("#editPromo").hide();
	$("#promo_content").slideDown('slow', function(){
		$("#add_promo_form").show();
		promo();
	});	
}